﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using control.Biz;
using control;

namespace control.Dal
{
    class DalKhachHang
    {
        internal static List<control.Biz.BizKhachHang> GetAll()
        {
            List<BizKhachHang> result = new List<BizKhachHang>();

            var khs = from kh in DalCommon.SachDB.KhachHangs
                      select new BizKhachHang(kh.IDKhachHang, kh.MaKhachHang, kh.TenDN, kh.MatKhau, kh.TenKH, kh.DiaChi, kh.Email,(int)kh.SDT, new BizQuyen(kh.Quyen.MaQuyen,kh.Quyen.TenQuyen));
            result = khs.ToList<BizKhachHang>();

            return result;
        }

        //internal static List<BizQuyen> GetAllQuyen()
        //{
        //    var qs = from q in DalCommon.SachDB.Quyens
        //             select new BizQuyen(q.MaQuyen, q.TenQuyen);

            
        //    return qs.ToList<BizQuyen>();
        //}


        internal static int Add(BizKhachHang bizKhachHang)
        
           
        {
            int result = bizKhachHang.IDKhachHang;
            KhachHang kh = new KhachHang();
            kh.MaKhachHang = bizKhachHang.MaKhachHang;
            kh.TenDN = bizKhachHang.TenDN;
            kh.MatKhau = bizKhachHang.Matkhau;
            kh.TenKH = bizKhachHang.TenKH;
            kh.DiaChi = bizKhachHang.Diachi;
            kh.Email = bizKhachHang.Email;
            kh.SDT = bizKhachHang.SDT;
           
           kh.MaQuyen = bizKhachHang.Quyen.MaQuyen;
            

            DalCommon.SachDB.KhachHangs.InsertOnSubmit(kh);
            DalCommon.SachDB.SubmitChanges();

            result = kh.IDKhachHang;

            return result;

           
        }







        internal static void Delete(BizKhachHang bizKhachHang)
        {
            //KhachHang kh = DalCommon.SachDB.KhachHangs.Single<KhachHang>(p => p.IDKhachHang == bizKhachHang.IDKhachHang);
            var kh = from c in DalCommon.SachDB.KhachHangs
                     where c.IDKhachHang == bizKhachHang.IDKhachHang
                     select c;

            DalCommon.SachDB.KhachHangs.DeleteAllOnSubmit<KhachHang>(kh);
            DalCommon.SachDB.SubmitChanges();
            
        }
        internal static void Updated(BizKhachHang bizKhachHang)
        {
            KhachHang kh = DalCommon.SachDB.KhachHangs.Single<KhachHang>(k => k.IDKhachHang == bizKhachHang.IDKhachHang);
            kh.MaKhachHang = bizKhachHang.MaKhachHang;
            kh.TenDN = bizKhachHang.TenDN;
            kh.MatKhau = bizKhachHang.Matkhau;
            kh.TenKH = bizKhachHang.TenKH;
            kh.DiaChi = bizKhachHang.Diachi;
            kh.Email = bizKhachHang.Email;
            kh.SDT = bizKhachHang.SDT;
            
            DalCommon.SachDB.SubmitChanges();
                      
           
        }



        internal static BizKhachHang DangNhap(string _username, string _password)
        {
            var users = from us in DalCommon.SachDB.KhachHangs
                        where us.TenDN==_username && us.MatKhau == _password
                        select new BizKhachHang(us.IDKhachHang, us.MaKhachHang, us.TenDN, us.MatKhau, us.TenKH, us.DiaChi, us.Email,(int)us.SDT, new BizQuyen(us.Quyen.MaQuyen,us.Quyen.TenQuyen));
            return users.FirstOrDefault<BizKhachHang>();
        }

        internal static bool changeInfo(BizKhachHang bizKhachHang)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            KhachHang kh = db.KhachHangs.Single(k => k.IDKhachHang == bizKhachHang.IDKhachHang);
            kh.MaKhachHang = bizKhachHang.MaKhachHang;
            kh.TenDN = bizKhachHang.TenDN;
            kh.MatKhau = bizKhachHang.Matkhau;
            kh.TenKH = bizKhachHang.TenKH;
            kh.DiaChi = bizKhachHang.Diachi;
            kh.Email = bizKhachHang.Email;
            kh.SDT = bizKhachHang.SDT;
            kh.MaQuyen = bizKhachHang.Quyen.MaQuyen;

            DalCommon.SachDB.SubmitChanges();
            return true;
            
        }

        internal static int Insert(BizKhachHang bizKhachHang)
        {
            try
            {
                Sach_WebDataContext db = DalCommon.SachDB;
                KhachHang kh = new KhachHang()
                {
                    MaKhachHang = bizKhachHang.MaKhachHang,
                    TenDN = bizKhachHang.TenDN,
                    MatKhau = bizKhachHang.Matkhau,
                    TenKH=bizKhachHang.TenKH,
                    Email = bizKhachHang.Email,
                    SDT = bizKhachHang.SDT,
                    DiaChi = bizKhachHang.Diachi,
                    MaQuyen = bizKhachHang.Quyen.MaQuyen,
                };
                db.KhachHangs.InsertOnSubmit(kh);
                db.SubmitChanges();
                return kh.IDKhachHang;
            }
            catch { return -1; }
        }

        internal static BizKhachHang FindByUsername(string _username)
        {
            var users = from us in DalCommon.SachDB.KhachHangs
                        where us.TenDN == _username 
                        select new BizKhachHang(us.IDKhachHang, us.MaKhachHang, us.TenDN, us.MatKhau, us.TenKH, us.DiaChi, us.Email, (int)us.SDT, new BizQuyen(us.Quyen.MaQuyen, us.Quyen.TenQuyen));
            return users.FirstOrDefault<BizKhachHang>();
        }

        internal static BizKhachHang GetByID(int p)
        {
            var users = from us in DalCommon.SachDB.KhachHangs
                        where us.IDKhachHang==p
                        select new BizKhachHang(us.IDKhachHang, us.MaKhachHang, us.TenDN, us.MatKhau, us.TenKH, us.DiaChi, us.Email, (int)us.SDT, new BizQuyen(us.Quyen.MaQuyen, us.Quyen.TenQuyen));
            return users.FirstOrDefault<BizKhachHang>();
        }
        
    }
}
    

    

